package com.smart.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smart.platform.common.Result;
import com.smart.platform.entity.TextTotal;
import com.smart.platform.service.TextTotalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/textTotal")
//@Api(tags = "评阅总任务添加接口")
public class TextTotalController {
    @Autowired
    private TextTotalService textTotalService;

    /**
     * sate course grade numberTotal scoreTotal reader
     *
     * @param textTotal
     * @return
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody TextTotal textTotal) {
        log.info(textTotal.toString());

        textTotalService.save(textTotal);

        return Result.success("批阅任务添加成功");
    }

    /**
     * state course grade
     *
     * @return
     */
    @DeleteMapping
    public Result<String> delete(Long id) {
        log.info("删除的批阅任务：{}", id);
        LambdaQueryWrapper<TextTotal> queryWrapper = new LambdaQueryWrapper<>();

        //根据state course grade reader 精确删除某次批阅任务
      /*  queryWrapper.eq(TextTotal::getState, textTotal.getState())
                .eq(TextTotal::getCourse, textTotal.getCourse())
                .eq(TextTotal::getGrade, textTotal.getGrade())
                .eq(TextTotal::getReader, textTotal.getReader());*/


        textTotalService.removeById(id);

        return Result.success("批阅任务删除成功");
    }

    /**
     * 根据当前session获取到的 userid 查看他当前所有的批阅任务
     *
     * @param request
     * @return
     */
    @GetMapping
    public Result<List<TextTotal>> getTexts(HttpServletRequest request) {
        String reader = (String) request.getSession().getAttribute("userId");

        LambdaQueryWrapper<TextTotal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TextTotal::getReader, reader);

        List<TextTotal> list = textTotalService.list(queryWrapper);

        return Result.success(list);
    }

    /**
     * 传过来修改的id 需要需改的数据：都可以改
     *
     * @param textTotal
     * @return
     */
    @PutMapping("update")
    public Result<String> update(@RequestBody TextTotal textTotal) {
        log.info("修改后的信息:{}", textTotal.toString());

        textTotalService.updateById(textTotal);

        return Result.success("批阅任务修改成功");
    }

}
